package com.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.service.UserService;
import com.service.impl.UserServiceImpl;


@Controller
public class UserController {
	@Autowired
	public UserService service;
	
	@RequestMapping(value="index.do")
	public void Test(){
//		service.test();
	}
	/**
	 * 
	 * verifyUser:验证用户登陆
	 * 
	 * @author 梁中华
	 * @param uname
	 * @param upwd
	 * @return
	 * @since 　Ver 1.1
	 */
	@RequestMapping(value="verifyUser.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String verifyUser(String uname,String upwd){
		Map<String,String> map=new HashMap<String,String>();
		map.put("uname", uname);
		map.put("upwd", upwd);
		return String.valueOf(service.verifyUser(map));
	}
	
	@RequestMapping(value="addUser.do",produces="text/html;charset=UTF-8")
	public String addUser(String uname,String upwd){
		
		
		return "";
	}
	RequestDispatcher rd=null;
	@RequestMapping(value="login.do")
	public void login(String uname,String upwd,String return_url,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		System.out.println(uname+"   "+upwd);
		if(uname.equals("admin")&&upwd.equals("admin")){
			request.getSession().setAttribute("flag", "login_success");
			if(return_url!=null){
				System.out.println("1");
				rd=request.getRequestDispatcher(return_url);
				rd.forward(request, response);
			}else{
				System.out.println("2");
				rd=request.getRequestDispatcher("/index.jsp");
				rd.forward(request, response);
			}
		}else{
			System.out.println("3");
			request.getSession().setAttribute("flag", "login_error");
			request.setAttribute("msg", "用户名错误");
			rd=request.getRequestDispatcher("/login.jsp");
			rd.forward(request, response);
		}
	}
	
	public void login_out(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath()+"/index.jsp");
	}
}
